package com.classtimer.config;

import com.classtimer.Model._MappingKit;
import com.classtimer.Routes.TimerRoutes;
import com.classtimer.Tasks.ClearDatabase;
import com.classtimer.Tasks.SendEmailJob;
import com.classtimer.modelutils.Cron4jPlugIn;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;

public class ProjectConfig extends JFinalConfig{
	
	Routes route;
	
	/**
	 * 环境变量配置
	 * 优先加载生产环境配置
	 * @param pro 生产环境配置文件名
	 * @param dev 开发环境配置文件名
	 */
	public static void loadProp(String pro,String dev) {
		try {
			PropKit.use(pro);
		}catch(Exception e) {
			PropKit.use(dev);
		}
	}
	
	@Override
	public void configConstant(Constants me) {
		// TODO Auto-generated method stub
		loadProp("pro_config.txt","dev_config.txt");
		me.setDevMode(PropKit.getBoolean("devMode",false));
		me.setEncoding("UTF-8");
		
		me.setViewType(ViewType.JFINAL_TEMPLATE);
		
	}

	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
		this.route = me;
		me.add(new TimerRoutes());
	}

	@Override
	public void configEngine(Engine me) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configPlugin(Plugins me) {
		// TODO Auto-generated method stub
		DruidPlugin druidPluin = new DruidPlugin(PropKit.get("MyjdbcUrl"), PropKit.get("username"), PropKit.get("password"));
		me.add(druidPluin);
		
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPluin);
		_MappingKit.mapping(arp);
		me.add(arp);
		loadPropertyFile("a_little_config.txt");
		Cron4jPlugIn cron4jPlugIn = new Cron4jPlugIn();
		if (getPropertyToBoolean("sendEmail.enable") == true) {
			cron4jPlugIn.addTask(getProperty("sendEmail.cron"), new SendEmailJob());
		}
		if (getPropertyToBoolean("clearDatabase.enable") == true) {
			cron4jPlugIn.addTask(getProperty("clearDatabase.cron"), new ClearDatabase());
		}
		me.add(cron4jPlugIn);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub
		
	}
	
	public static void main(String[] args) {
		JFinal.start("WebRoot",8188,"/");
	}

}
